package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.qo.CustomerReportQueryObject;
import cn.wolfcode.crm.service.ICustomerReportService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model){
        List<Map<String, Object>> maps = customerReportService.selectCustomerNumberGroupByType(qo);
        model.addAttribute("maps",maps);
        return "customerReport/customerReport";
    }

    @RequestMapping("/chartByBar")
    public String chartByBar(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model){
        //获取所有报表数据
        List<Map<String, Object>> maps = customerReportService.selectCustomerNumberGroupByType(qo);
        //分开xy轴的数据, x轴是类型   y是数据
        List<Object> groupTypes = new ArrayList<>();
        List<Object> numbers = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            groupTypes.add(map.get("groupType"));
            numbers.add(map.get("number"));
        }
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("numbers",JSON.toJSONString(numbers));
        return "customerReport/customerReportChartByBar";
    }




    @RequestMapping("/chartByPie")
    public String chartByPie(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model){
        //获取所有报表数据
        List<Map<String, Object>> maps = customerReportService.selectCustomerNumberGroupByTypeForPie(qo);
        //分开xy轴的数据, x轴是类型   y是数据
        List<Object> names = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            names.add(map.get("name"));
            values.add(map.get("value"));
        }
        model.addAttribute("names", JSON.toJSONString(names));
        model.addAttribute("values",JSON.toJSONString(values));
        model.addAttribute("maps",JSON.toJSONString(maps));
        return "customerReport/customerReportChartByPie";
    }




}
